package com.ruoyi.web.controller.serve;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.serve.domain.ServerUrl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.serve.domain.ServerInfo;
import com.ruoyi.serve.service.IServerInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 服务列表Controller
 * 
 * @author liu
 * @date 2023-11-09
 */
@RestController
@RequestMapping("/serve/serveInfo")
public class ServerInfoController extends BaseController {
    @Autowired
    private IServerInfoService serverInfoService;

    /**
     * 查询服务列表列表
     */
    @PreAuthorize("@ss.hasPermi('serve:serveInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ServerInfo serverInfo) {
        startPage();
        List<ServerInfo> list = serverInfoService.selectServerInfoList(serverInfo);
        return getDataTable(list);
    }

    /**
     * 导出服务列表列表
     */
    @PreAuthorize("@ss.hasPermi('serve:serveInfo:export')")
    @Log(title = "服务列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServerInfo serverInfo) {
        List<ServerInfo> list = serverInfoService.selectServerInfoList(serverInfo);
        ExcelUtil<ServerInfo> util = new ExcelUtil<ServerInfo>(ServerInfo.class);
        util.exportExcel(response, list, "服务列表数据");
    }

    /**
     * 获取服务列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('serve:serveInfo:query')")
    @GetMapping(value = "/{serverId}")
    public AjaxResult getInfo(@PathVariable("serverId") Long serverId) {
        return success(serverInfoService.selectServerInfoByServerId(serverId));
    }

    /**
     * 新增服务列表
     */
    @PreAuthorize("@ss.hasPermi('serve:serveInfo:add')")
    @Log(title = "服务列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServerInfo serverInfo) {
        return toAjax(serverInfoService.insertServerInfo(serverInfo));
    }

    /**
     * 修改服务列表
     */
    @PreAuthorize("@ss.hasPermi('serve:serveInfo:edit')")
    @Log(title = "服务列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServerInfo serverInfo) {
        return toAjax(serverInfoService.updateServerInfo(serverInfo));
    }

    /**
     * 删除服务列表
     */
    @PreAuthorize("@ss.hasPermi('serve:serveInfo:remove')")
    @Log(title = "服务列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{serverIds}")
    public AjaxResult remove(@PathVariable Long[] serverIds) {
        return toAjax(serverInfoService.deleteServerInfoByServerIds(serverIds));
    }

    /**
     * 端口查询
     **/
    @Log(title = "端口查询", businessType = BusinessType.OTHER)
    @PostMapping("/getUsedPorts")
    public List<Integer> getUsedPorts(@RequestBody ServerInfo serverInf) {
        return serverInfoService.getUsedPorts(serverInf);
    }


    /**
     * @Description: 统计服务器汇总
     * @Author: xrw
     **/
    @Log(title = "服务器汇总查询", businessType = BusinessType.OTHER)
    @GetMapping("/countList")
    public AjaxResult countList() {
        List<ServerInfo> list = serverInfoService.selectServerInfoList(null);

        Map<String, Object> map = new HashMap<>();

        List<ServerInfo> abnormalServerInfo = new ArrayList<>();

        // 获取异常的网站
        for (ServerInfo info : list) {
            if (info.getIsPing()==0) {
                abnormalServerInfo.add(info);
            }
        }

        map.put("total", list.size());
        map.put("abnormalTotal", abnormalServerInfo.size());
        map.put("abnormalServerInfo", abnormalServerInfo);

        return success(map);
    }

}
